package 多线程进阶;
/**
 * 创建线程计算 1 + 2 + 3 + ... + 1000, 不使用 Callable 版本
 */
public class TestDemo2 {
    static class Result{
        int sum=0;
        Object locker=new Object();
    }
    public static void main(String[] args) throws InterruptedException {
        Result result=new Result();
        Thread thread=new Thread(()->{
            int sum=0;
            for (int i = 0; i <= 100; i++) {
                sum+=i;
            }
            synchronized (result.locker){
                result.sum=sum;
                result.locker.notify();
            }
        });
        thread.start();
        synchronized (result.locker){
            while(result.sum==0){
                result.locker.wait();
            }
        }
        System.out.println(result.sum);
    }
}
